// components/floatBall/index.js
const app = getApp()

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    page:{
      type:String,
      value:''
    },
    isTabBar:{
      type:String,
      value:true
    },
    navHeight: {
      type:Number,
      value: 44 + wx.getSystemInfoSync().statusBarHeight,
    },
    tabBarHeight:{
      type:Number,
      value:44
    }
  },

  /**
   * 组件的初始数据
   */
 data:{
    // 浮动按钮样式
    buttonTop: app.globalData.screenHeight / 2,
    buttonLeft: wx.getSystemInfoSync().screenWidth - 70,
    windowHeight: wx.getSystemInfoSync().screenHeight - 64,
    windowWidth: wx.getSystemInfoSync().screenWidth,
    startPoint: ""
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
	 * 拖动浮动
	 */
    buttonStart: function (e) {
      this.setData({
        startPoint: e.touches[0]
      })
    },

    buttonMove: function (e) {
      
      var startPoint1 = this.data.startPoint;
      var endPoint = e.touches[e.touches.length - 1];
      var translateX = endPoint.clientX - startPoint1.clientX;
      var translateY = endPoint.clientY - startPoint1.clientY;
    
      this.setData({
        startPoint: endPoint
      })

      var buttonTop = this.data.buttonTop + translateY;
      var buttonLeft = this.data.buttonLeft + translateX;

      //判断是移动否超出屏幕
      if (buttonLeft + 70 >= this.data.windowWidth) 
      {
         buttonLeft = this.data.windowWidth - 70;
      }
      
      // 
      if (buttonLeft <= 0) {
        buttonLeft = 0;
      }
      
      // 处理顶部越界问题
      if (buttonTop <= this.data.navHeight) {
        buttonTop = this.data.navHeight
      }
      
      // 处理底部越界问题
      if (buttonTop + this.data.tabBarHeight >= this.data.windowHeight) {
        buttonTop = this.data.windowHeight - this.data.tabBarHeight;
      }

      this.setData({
        buttonTop: buttonTop,
        buttonLeft: buttonLeft
      })

    },


    ballTap:function(){
      app.globalData.toCrmPage = this.data.page
      app.globalData.isTabBar = this.data.isTabBar
      // let url = encodeURIComponent('https://crmcs.quanyou.com.cn//appweb/#/order/sign-up?token=7598dc0d-cdfb-4aa0-b2fb-c504e81623fd&phone=&thirdSource=MallRegistration&provinceCode=101&cityCode=102&districtCode=103&streetCode=123444&storeCode=104')
      wx.navigateTo({
        url: '/packagBuy/pages/found/crmweb/index',
      })
    }
  }
})
